// stucture/calc-mvc/CalcController.java - Controller
//    Handles user interaction with listeners.
//    Calls View and Model as needed.
// Fred Swartz -- December 2004

import java.awt.event.*;

public class MCQcontroller {
    //... The Controller needs to interact with the View.
    private MCQview  mcq_view;
   // Tracks the current question number
    private Jtable table_view;
    private int _questionTracker; 
   // Holds all the answers selected by user
    private String answers[];
   // Holds the current answer
    private String _answer;
   // for testing purposes
    private String question1[] = {"Who is the richest person in the world","Thato Selebogo","Bill Gates","Nelson Mandela","Manny Pacqiou"};
    private String question2[] = {"Who is the lecture of Software Development III","Thato Selebogo","Aveer Ramnath","Ken Nixon","Manny Pacqiou"};
    private String question3[] = {"PHP stands for:","People Hire People","Pick han Pay", "PHP hyper processor", "PHP"};
    /** Constructor */
    MCQcontroller(MCQview view, Jtable tableview) {
        mcq_view  = view;
	table_view = tableview;
	//Start at question 1
	_questionTracker = 1;
	
	answers = new String[20];
        
        //... Add listeners to the view.
        mcq_view.addTestBeginListener(new TestBeginListener());
        mcq_view.addTestFinishListener(new TestFinishListener());
	mcq_view.addNextQListener(new NextQListener());
	mcq_view.addPreviousQListener(new PreviousQListener());
	mcq_view.addQSelectListener(new QSelectListener());
    }


    // return selected answer
    public String getAnswer()
    {
	return _answer;
    }
    
    
    ////////////////////////////////////////// inner class TestBeginListener

    class TestBeginListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
	
	mcq_view.initComponents();
	/*mcq_view.populateView(retriveQuestion(_questionTracker));*/
	mcq_view.beginTestButton.setVisible(false);
	mcq_view.populateView(question1);
        }
    }//end inner class TestBeginListener
    
   ////////////////////////////////////////// inner class QSelectListener

    class QSelectListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
	
        _answer = e.getActionCommand();

	/*if(questionTracker == 20)
	{
		
	}*/
        }
    }//end inner class QSelectListener

   ////////////////////////////////////////// inner class PreviousQListener

    class PreviousQListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
	
	/*questionTracker = questionTracker - 1;
        remove[answers,questionTracker];
	populateView(retriveQuestion(_questionTracker));
	questionTracker = questionTracker + 1;*/
	mcq_view.populateView(question3);
        }
    }//end inner class PreviousQListener

   ////////////////////////////////////////// inner class NextQListener

    class NextQListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
	
	/*populateView(retriveQuestion(_questionTracker));*/
	
	mcq_view.questionNumber = mcq_view.questionNumber + 1;
	mcq_view.populateView(question2);
	answers[_questionTracker] = _answer;
	_questionTracker = _questionTracker + 1;
	System.out.println(_answer);
        }
    }//end inner class NextQListener

   ////////////////////////////////////////// inner class TestFinishListener

    class TestFinishListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
	
	/*String dbAnswers[] = retrieveDBAnswers();
	String results[] = compare(answers,dbAnswers);
	mcq_view.displayResults(results);*/
	//mcq_view.layout.setVisible(false);
	mcq_view.setVisible(false);
	table_view.setExtendedState(table_view.MAXIMIZED_BOTH); 
	table_view.setVisible(true);

        }
    }//end inner class TestFinishListener
}

